package com.flightgame.controllers;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.flightgame.services.JSONServices;

@Controller
@RequestMapping("/json")
public class JsonController {
	
	@Autowired
	private JSONServices jsonServices;
	
	// Airports
	
	@RequestMapping(value = "/airports", method = RequestMethod.GET)
	public void airports(HttpServletRequest request,HttpServletResponse response) throws IOException{
		Writer writer = response.getWriter();
		
		writer.write(jsonServices.getAirports().toString());
		response.setCharacterEncoding("UTF-8");
	}
	
	@RequestMapping(value = "/airports/{lat1}/{lat2}/{lon1}/{lon2}", method = RequestMethod.GET)
	public void airportsOnboundedCoordinates(@PathVariable String lat1,@PathVariable String lat2,@PathVariable String lon1,@PathVariable String lon2,HttpServletRequest request,HttpServletResponse response) throws IOException{
		Writer writer = response.getWriter();
		writer.write(jsonServices.getAirportsOnBoundedCoordinates(lat1,lat2,lon1,lon2).toString());
		response.setCharacterEncoding("UTF-8");
	}
	
	// Flights
	
	@RequestMapping(value = "/flights/{lat1}/{lat2}/{lon1}/{lon2}", method = RequestMethod.GET)
	public void flightsOnboundedCoordinates(@PathVariable String lat1,@PathVariable String lat2,@PathVariable String lon1,@PathVariable String lon2,HttpServletRequest request,HttpServletResponse response) throws IOException{
		Writer writer = response.getWriter();
		
		writer.write(jsonServices.getFlightsOnBoundedCoordinates(lat1,lat2,lon1,lon2).toString());
		response.setCharacterEncoding("UTF-8");
	}
	
	@RequestMapping(value = "/flight-history/{ownershipId}", method = RequestMethod.GET)
	public void flightHistory(@PathVariable String ownershipId,HttpServletRequest request,HttpServletResponse response) throws IOException{
		Writer writer = response.getWriter();
		
		writer.write(jsonServices.getFlightHistory(Integer.parseInt(ownershipId)).toString());
		response.setCharacterEncoding("UTF-8");
	}
	
	// Ownerships
	
	@RequestMapping(value = "/ownershipsaround/{lat1}/{lon1}/{lat2}/{lon2}", method = RequestMethod.GET)
	public void ownershipsOnboundedCoordinates(@PathVariable String lat1,@PathVariable String lat2,@PathVariable String lon1,@PathVariable String lon2,HttpServletRequest request,HttpServletResponse response) throws IOException{
		Writer writer = response.getWriter();
		
		writer.write(jsonServices.getOwnershipsOnBoundedCoordinates(lat1,lat2,lon1,lon2).toString());
		response.setCharacterEncoding("UTF-8");
	}
	
	@RequestMapping(value = "/ownerships/{username}", method = RequestMethod.GET)
	public void ownershipsOfUser(@PathVariable String username,HttpServletRequest request,HttpServletResponse response){
		Writer writer;
		try {
			writer = response.getWriter();
			writer.write(jsonServices.getUserOwnerships(username).toString());
			response.setCharacterEncoding("UTF-8");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	// User
	@RequestMapping(value = "/user/{username}", method = RequestMethod.GET)
	public void getUserData(@PathVariable String username,HttpServletRequest request,HttpServletResponse response) throws IOException{
		Writer writer = response.getWriter();
		writer.write(jsonServices.getUserData(username).toString());
		response.setCharacterEncoding("UTF-8");
	}
	
	// Aircrafts
	@RequestMapping(value = "/aircraft-fuel/{ownershipId}", method = RequestMethod.GET)
	public void aircraftFuel(@PathVariable String ownershipId,HttpServletRequest request,HttpServletResponse response) throws IOException{
		Writer writer = response.getWriter();
		
		writer.write(jsonServices.getFuelOfAircraft(ownershipId).toString());
		response.setCharacterEncoding("UTF-8");
	}
	
}
